package com.itheima.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    //调用业务层
    private UserService userService = new UserServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取登陆页面的用户名和密码
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //将用户名和密码传递给业务层
        User u = userService.login(username,pwd);
        //对获取的用户信息做出判断
        if(u!=null){
            //登录成功
            HttpSession session = req.getSession();
            session.setAttribute("user",u);
            resp.sendRedirect(req.getContextPath()+"/main.jsp");
        }else {
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
    }
}
